import java.awt.Point;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Random;


public class RandomWalkSquare {
	
	static String directory = "C:/Users/accufind/Desktop/Files/";
	
	Point[] path;
	int squareSize;
	int numSteps;
	int step;

	/*constructor*/
	public RandomWalkSquare(int squareSize, int numSteps, int step){
		this.squareSize = squareSize;
		this.numSteps = numSteps;
		this.step = step;
	}

	//write the path to a txt file and build path[], path[] is returned for drawing image
	public Point[] walkRandomly(int objectIndex, FileWriter writer, Person walker, int[][] map) throws IOException{
		
		int count = 0; // for numSteps
		int option = 0; //for direction
		int step = 25; //no use, just for the minimum stepSize
    	
    	System.out.println("writing path file:"+(walker.index+1)+".txt ...");
    	
    	Random rand = new Random(); // for random number of timestamp and direction
    	
    	//timestamp
    	int hour = rand.nextInt(11)+10;//10:00-20:59 +[0:15,3:00]
    	int minute = rand.nextInt(60);
    	int second = rand.nextInt(60);
    	int milisecond = rand.nextInt(1000);
    	Timestamp timestamp = new Timestamp(0);
    	timestamp = Timestamp.valueOf("2014-1-31"+" "+hour+":"+minute+":"+second+"."+milisecond);
    	
		//path.txt
    	//System.out.println(objectIndex);
    	writer.append("Object "+objectIndex+","+timestamp.toString()+","+walker.x+","+walker.y+"\n");
        //path[]
    	path = new Point[numSteps+1];
    	path[0]=new Point(walker.x,walker.y); 	
    	
    	//walk in random direction and random stepSize
    	int previousDirection=rand.nextInt(8);//this is the start direction
    	
    	//give each direction different probabilities,
    	//but here only 5 directions are used
		while(count<numSteps){		
			double num = rand.nextGaussian();//highlight
	    	if((num>-1)&&(num<1)) option = previousDirection;//NORTH(0, -1)
	    	else if ((num>-2)&&(num<-1)) option = (previousDirection+1)%8;//EAST(1, 0)
	    	else if ((num>-3)&&(num<-2)) option = (previousDirection+4)%8;//NW(-1,-1)
	    	else if ((num>1)&&(num<2)) option = (previousDirection+3)%8;//WEST(-1, 0)
	    	else if ((num>2)&&(num<3)) option = (previousDirection+5)%8;// NE(1,-1)
			
            Directions direction = Directions.values()[option];
			previousDirection = option;
			
			int stepSize = rand.nextInt(11)+step;//25-35
			
			if(walker.checkAvailability(stepSize, map, direction)){
				for(int i=0;i<stepSize;i++) walker.move(direction);		
				count++;
				//timestamp
				timestamp.setTime(timestamp.getTime()+1200);
				//path.txt
		    	writer.append("Object "+objectIndex+","+timestamp.toString()+","+walker.x+","+walker.y+"\n");				
				//path[]
				path[count]=new Point(walker.x,walker.y);
			} 
		}//finish the numSteps walk
	    
	    return path;//path[]
	}
}
